package com.lc.luzhengtang.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.lc.luzhengtang.R;
import com.lc.luzhengtang.adapter.CommentSectionAdapter2;
import com.lc.luzhengtang.base.BaseActivity;
import com.lc.luzhengtang.bean.CommentDetailBean;
import com.lc.luzhengtang.conn.ConnCommentDetail;
import com.zcx.helper.http.AsyCallBack;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


/**
 * Created by Administrator on 2017/5/5.
 */
public class CommentOrderActivity extends BaseActivity implements SwipeRefreshLayout.OnRefreshListener {


    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.rv_list)
    RecyclerView rvList;
    @BindView(R.id.swipeLayout)
    SwipeRefreshLayout swipeLayout;

    CommentSectionAdapter2 commentSectionAdapter;
    private List<CommentDetailBean.DataBean> mData;
    List<CommentDetailBean.DataBean> results;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_commentlist_detail);
        ButterKnife.bind(this);
        mData = new ArrayList<>();
        swipeLayout.setOnRefreshListener(this);
        swipeLayout.setColorSchemeColors(Color.rgb(47, 223, 189));
        rvList.setLayoutManager(new LinearLayoutManager(this));
        initData();
    }

    protected void initData() {
        rvList.setItemAnimator(new DefaultItemAnimator());
        //recyclerView.setLayoutManager(new LinearLayoutManager(context));
        rvList.setLayoutManager(new LinearLayoutManager(this));
        commentSectionAdapter = new CommentSectionAdapter2(null);
        rvList.setAdapter(commentSectionAdapter);
        commentSectionAdapter.openLoadAnimation(BaseQuickAdapter.SCALEIN);
        commentSectionAdapter.isFirstOnly(false);


        swipeLayout.setColorSchemeColors(Color.RED, Color.BLUE, Color.GREEN);
        swipeLayout.setOnRefreshListener(this);
        //开启loading,获取数据
        setRefreshing(true);
        onRefresh();
        commentSectionAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                Intent intent = new Intent();

                intent.putExtra("evaluate_id", results.get(position).getEvaluate_id() + "");
                intent.putExtra("img_url", results.get(position).getPicurl());
                intent.setClass(CommentOrderActivity.this, CommentChangeActivity.class);
                startActivity(intent);


            }
        });
    }

    public void setRefreshing(final boolean refreshing) {
        swipeLayout.post(new Runnable() {
            @Override
            public void run() {
                swipeLayout.setRefreshing(refreshing);
            }
        });
    }


    @Override
    public void onRefresh() {
        new ConnCommentDetail(getIntent().getStringExtra("ordersn"), new AsyCallBack<CommentDetailBean>() {
            @Override
            public void onStart(int type) throws Exception {
                super.onStart(type);

            }

            @Override
            public void onSuccess(String toast, int type, Object o, CommentDetailBean commenBean) throws Exception {
                super.onSuccess(toast, type, o, commenBean);
               /* if (commenBean != null) {
                    mData.clear();
                    mData.addAll(commenBean.getData());
                    commentSectionAdapter = new CommentSectionAdapter2(commenBean.getData());
                    rvList.setAdapter(commentSectionAdapter);
                }*/

                results = commenBean.getData();
                if (results != null) {
                    commentSectionAdapter.setNewData(results);
                }

            }

            @Override
            public void onFail(String toast, int type) throws Exception {
                super.onFail(toast, type);
            }

            @Override
            public void onEnd(String toast, int type) throws Exception {
                super.onEnd(toast, type);
                setRefreshing(false);
            }
        }

        ).execute(CommentOrderActivity.this, false);
    }

    @OnClick(R.id.iv_back)
    public void onViewClicked() {
        finish();
    }
}
